﻿using WeDonekRpc.Client;
using RpcManageClient;
using RpcStore.Collect;
using RpcStore.Model.DB;
using RpcStore.RemoteModel.ClientLimit.Model;
using RpcStore.Service.Interface;
using System.Collections.Generic;

namespace RpcStore.Service.lmpl
{
    internal class ClientLimitService : IClientLimitService
    {
        private readonly IServerClientLimitCollect _Limit = null;
        private IRpcServerCollect _RpcServer;
        public ClientLimitService(IServerClientLimitCollect limit, IRpcServerCollect rpcServer)
        {
            _RpcServer= rpcServer;
            _Limit = limit;
        }
        public void Delete(long id)
        {
            ServerClientLimitModel limit = _Limit.Get(id);
            _Limit.Delete(limit);
            _RpcServer.RefreshClientLimit(limit.RpcMerId, limit.ServerId);
        }

        public ClientLimitData Get(long rpcMerId, long serverId)
        {
            ServerClientLimitModel limit = _Limit.Get(rpcMerId, serverId);
            if (limit == null)
            {
                return null;
            }
            return limit.ConvertMap<ServerClientLimitModel, ClientLimitData>();
        }

        public void Sync(ClientLimitDatum add)
        {
            if (_Limit.Sync(add))
            {
                _RpcServer.RefreshClientLimit(add.RpcMerId, add.ServerId);
            }
        }
    }
}
